<?php


namespace Part02_StrategyMode\Code6;


use Exception;


class CashContext
{
    private $cashObj;

    /**
     * 实例化不传入一个对象，而是一个类型
     * CashContext constructor.
     * @param $type
     * @throws \Exception
     */
    public function __construct($type)
    {
        switch ($type) {
            case '正常收费':
                $this->cashObj = new CashNormal();
                break;
            case '满300返100':
                $this->cashObj = new CashReturn(300, 100);
                break;
            case '打8折':
                $this->cashObj = new CashRebate(0.8);
                break;
            default:
                throw new Exception('未知折扣');
        }
    }

    /**
     * @param $money
     * @return float|int
     * @date: 2021/9/10
     * @desc: 根据策略的不同，获得计算结果
     */
    public function GetResult($money)
    {
        return $this->cashObj->acceptCash($money);
    }

}